package com.xiaoyang.gatewayserver.filter;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.util.HashMap;


/**
 * @auther XiaoYang
 * @DATE 2023/5/6-19:35
 * 定义了一个过滤器
 * 但是未排序
 */

@Component
public class MyGlobalFilter implements GlobalFilter, Ordered {

    /**
     * 这个就是过滤的方法
     * 责任链模式
     * 常见使用： 网关里使用  mybatis的二级缓存机制有变种责任链模式
     *
     * @param exchange
     * @param chain
     * @return
     */
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {

        //针对请求的过滤  拿到请求 header url 参数
        ServerHttpRequest request = exchange.getRequest();
        // HttpServletRequest web里面的
        // ServerHttpRequest  webFlux里面的 响应式里面的
        String path = request.getURI().getPath();
        // /login-service/doLogin
        System.out.println(path);
        HttpHeaders headers = request.getHeaders();
        //[Host:"localhost", User-Agent:"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/112.0", Accept:"text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8", Accept-Language:"zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2", Accept-Encoding:"gzip, deflate, br", Connection:"keep-alive", Cookie:"Idea-9e1e26be=66bc5206-1f5b-4900-8fb2-396b9f66533c; Hm_lvt_c314395e62fb55e30828e0ea8cca8b3a=1672501336,1672502256,1672726887", Upgrade-Insecure-Requests:"1", Sec-Fetch-Dest:"document", Sec-Fetch-Mode:"navigate", Sec-Fetch-Site:"none", Sec-Fetch-User:"?1"]
        System.out.println(headers);
        String methodName = request.getMethod().name();
        //GET
        System.out.println(methodName);
        String hostName = request.getRemoteAddress().getHostName();
        //xiaoyang1 127.0.0.1
        System.out.println(hostName);

        //响应相关的数据
        ServerHttpResponse response = exchange.getResponse();

        /*
        //拦截
        //因为用了微服务一定是前后端分离的项目 所以前后端传输数据使用json 则可以返回{"code":200,"msg":"ok"}
        //1、设置编码 响应头里面设置
        response.getHeaders().set("content-type","application/json;charset=utf-8");
        //2、组装业务返回值
        HashMap<String, Object> map = new HashMap<>(4);
        map.put("code", HttpStatus.UNAUTHORIZED.value());
        map.put("msg","你未授权哟~");
        ObjectMapper objectMapper = new ObjectMapper();
        //把一个map 转成一个字节
        byte[] bytes = new byte[0];
        try {
            bytes = objectMapper.writeValueAsBytes(map);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        //把这个字节包装一下 通过buffer工厂将字节数组装成一个数据包
        DataBuffer wrap = response.bufferFactory().wrap(bytes);
        return response.writeWith(Mono.just(wrap));
         */



        //放行到下一个过滤器
        return chain.filter(exchange);


    }

    @Override
    public int getOrder() {
        return 0;
    }
}
